package com.hemaapp.yjnh.activity;

import java.util.ArrayList;

import xtom.frame.util.XtomToastUtil;
import xtom.frame.view.XtomRefreshLoadmoreLayout;
import xtom.frame.view.XtomRefreshLoadmoreLayout.OnStartListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hemaapp.yjnh.BaseActivity;
import com.hemaapp.yjnh.BaseHttpInformation;
import com.hemaapp.yjnh.R;
import com.hemaapp.yjnh.bean.Address;
import com.hemaapp.yjnh.bean.User;
import com.hemaapp.hm_FrameWork.HemaNetTask;
import com.hemaapp.hm_FrameWork.result.HemaBaseResult;
import com.hemaapp.hm_FrameWork.result.HemaPageArrayResult;
import com.hemaapp.hm_FrameWork.view.RefreshLoadmoreLayout;

/**收货地址
 * @author ZhangGuiyou
 * Date: 2015-07-30 13:55
 */
public class AddressActivity extends BaseActivity implements OnClickListener {

	private static final int REQUEST_MOD = 0x01;
	
	private TextView titleText;
	private ImageButton titleLeft;
	private Button titleRight;

	private XtomRefreshLoadmoreLayout layout;
	
	private LinearLayout ll_address1;
	private TextView tv_hint1;
	private LinearLayout ll_ad1_detail;
	private TextView ad1_name;
	private TextView ad1_tel;
	private TextView ad1_address;
	private ImageView iv_1;
	
	private LinearLayout ll_address2;
	private TextView tv_hint2;
	private LinearLayout ll_ad2_detail;
	private TextView ad2_name;
	private TextView ad2_tel;
	private TextView ad2_address;
	private ImageView iv_2;
	
	private LinearLayout ll_address3;
	private TextView tv_hint3;
	private LinearLayout ll_ad3_detail;
	private TextView ad3_name;
	private TextView ad3_tel;
	private TextView ad3_address;
	private ImageView iv_3;
	
	private ArrayList<Address> addresses = new ArrayList<Address>();
	private Address address1;
	private Address address2;
	private Address address3;
	private ArrayList<ImageView> images = new ArrayList<ImageView>();
	private int type = -1;//1从个人中心进来，2从确认订单进来
	private User user;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_address);
		super.onCreate(savedInstanceState);
		user = getApplicationContext().getUser();
		getAddressList();
	}
	
	private void getAddressList() {
		if (user != null) {
			getNetWorker().addressList(user.getToken());
		}
	}
	
	@Override
	protected void callBeforeDataBack(HemaNetTask netTask) {
		BaseHttpInformation info = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (info) {
		case ADDRESS_LIST:
			showProgressDialog("请稍候");
			break;
		default:
			break;
		}
	}

	@Override
	protected void callAfterDataBack(HemaNetTask netTask) {
		BaseHttpInformation info = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (info) {
		case ADDRESS_LIST:
			cancelProgressDialog();
			break;
		default:
			break;
		}
	}

	@Override
	protected void callBackForServerSuccess(HemaNetTask netTask,
			HemaBaseResult baseResult) {
		BaseHttpInformation info = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (info) {
		case ADDRESS_LIST:
			HemaPageArrayResult<Address> aResult = (HemaPageArrayResult<Address>) baseResult;
			layout.refreshSuccess();
			layout.setLoadmoreable(false);
			addresses.clear();
			addresses.addAll(aResult.getObjects());
			setData();
			break;
		default:
			break;
		}
	}

	@Override
	protected void callBackForServerFailed(HemaNetTask netTask,
			HemaBaseResult baseResult) {
		BaseHttpInformation info = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (info) {
		case ADDRESS_LIST:
			showTextDialog("数据获取失败\n请稍后再试！");
			titleText.postDelayed(new Runnable() {
				
				@Override
				public void run() {
					finish();
				}
			}, 500);
			break;
		default:
			break;
		}
	}

	@Override
	protected void callBackForGetDataFailed(HemaNetTask netTask, int failedType) {
		BaseHttpInformation info = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (info) {
		case ADDRESS_LIST:
			showTextDialog("数据获取失败\n请稍后再试！");
			titleText.postDelayed(new Runnable() {
				
				@Override
				public void run() {
					finish();
				}
			}, 500);
			break;
		default:
			break;
		}
	}
	
	private void setData() {
		int size = addresses.size();
		if (size == 0) {
			address1 = null;
			address2 = null;
			address3 = null;
		}else if (size == 1) {
			address1 = addresses.get(0);
			address2 = null;
			address3 = null;
		}else if (size == 2) {
			address1 = addresses.get(0);
			address2 = addresses.get(1);
			address3 = null;
		}else {//超过三个地址时，只显示前三个
			address1 = addresses.get(0);
			address2 = addresses.get(1);
			address3 = addresses.get(2);
		}
		
		if (address1 == null) 
			return;
		tv_hint1.setVisibility(View.GONE);
		ll_ad1_detail.setVisibility(View.VISIBLE);
		ad1_name.setText(address1.getName());
		ad1_tel.setText(address1.getTel());
		ad1_address.setText(address1.getPosition()+address1.getAddress());

		if (address2 == null) 
			return;
		tv_hint2.setVisibility(View.GONE);
		ll_ad2_detail.setVisibility(View.VISIBLE);
		ad2_name.setText(address2.getName());
		ad2_tel.setText(address2.getTel());
		ad2_address.setText(address2.getPosition()+address2.getAddress());
		
		if (address3 == null) 
			return;
		tv_hint3.setVisibility(View.GONE);
		ll_ad3_detail.setVisibility(View.VISIBLE);
		ad3_name.setText(address3.getName());
		ad3_tel.setText(address3.getTel());
		ad3_address.setText(address3.getPosition()+address3.getAddress());
	}

	@Override
	protected void findView() {
		titleText = (TextView) findViewById(R.id.title_text);
		titleLeft = (ImageButton) findViewById(R.id.title_left_btn);
		titleRight = (Button) findViewById(R.id.title_right_btn);

		layout = (RefreshLoadmoreLayout) findViewById(R.id.refreshLoadmoreLayout);
		
		ll_address1 = (LinearLayout) findViewById(R.id.ll_address_1);
		tv_hint1 = (TextView) findViewById(R.id.tv_hint1);
		ll_ad1_detail = (LinearLayout) findViewById(R.id.ll_ad1_detail);
		ad1_name = (TextView) findViewById(R.id.tv_name1);
		ad1_tel = (TextView) findViewById(R.id.tv_phone1);
		ad1_address = (TextView) findViewById(R.id.tv_address1);
		iv_1 = (ImageView) findViewById(R.id.iv_1);
		
		ll_address2=(LinearLayout) findViewById(R.id.ll_address_2);
		tv_hint2 = (TextView) findViewById(R.id.tv_hint2);
		ll_ad2_detail =(LinearLayout) findViewById(R.id.ll_ad2_detail);
		ad2_name = (TextView) findViewById(R.id.tv_name2);
		ad2_tel = (TextView) findViewById(R.id.tv_phone2);
		ad2_address = (TextView) findViewById(R.id.tv_address2);
		iv_2 = (ImageView) findViewById(R.id.iv_2);
		
		ll_address3=(LinearLayout) findViewById(R.id.ll_address_3);
		tv_hint3 = (TextView) findViewById(R.id.tv_hint3);
		ll_ad3_detail = (LinearLayout) findViewById(R.id.ll_ad3_detail);
		ad3_name = (TextView) findViewById(R.id.tv_name3);
		ad3_tel = (TextView) findViewById(R.id.tv_phone3);
		ad3_address = (TextView) findViewById(R.id.tv_address3);
		iv_3 = (ImageView) findViewById(R.id.iv_3);
	}

	@Override
	protected void getExras() {
		type = mIntent.getIntExtra("type", -1);
	}

	@Override
	protected void setListener() {
		titleText.setText("收货地址");
		titleLeft.setOnClickListener(this);
		titleRight.setText("编辑");
		titleRight.setOnClickListener(this);
		if (type == 1) {//个人中心
			titleRight.setVisibility(View.GONE);
		}else {
			titleRight.setVisibility(View.VISIBLE);
		}
		initImage();
		
		ll_address1.setOnClickListener(this);
		ll_address2.setOnClickListener(this);
		ll_address3.setOnClickListener(this);
		layout.setOnStartListener(new OnStartListener() {

            @Override
            public void onStartRefresh(XtomRefreshLoadmoreLayout v) {
                getAddressList();
            }

            @Override
            public void onStartLoadmore(XtomRefreshLoadmoreLayout v) {
            }
        });
	}
	
	/**初始化地址选择的右侧图标是圈圈还是箭头
	 * 
	 */
	private void initImage() {
		if (type == 1) {
			iv_1.setImageResource(R.drawable.arrow_right);
			iv_2.setImageResource(R.drawable.arrow_right);
			iv_3.setImageResource(R.drawable.arrow_right);
		}else {
			iv_1.setImageResource(R.drawable.select_n);
			iv_2.setImageResource(R.drawable.select_n);
			iv_3.setImageResource(R.drawable.select_n);
		}
		images.add(iv_1);
		images.add(iv_2);
		images.add(iv_3);
	}
	
	private void setImages(int position) {
		for (int i = 0; i < images.size(); i++) {
			images.get(i).setImageResource(R.drawable.select_n);
		}
		ImageView img = images.get(position);
		img.setImageResource(R.drawable.select_y_orange);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_left_btn:
			finish();
			break;
		case R.id.title_right_btn:
			if (type == 2) {//从选择收货地址中进来,点击了编辑，进行地址编辑，这里的操作和从个人中心中一样，故type设为1，按照个人中心中的操作来
				type = 1;
				initImage();
				titleRight.setText("完成");
				titleRight.setVisibility(View.VISIBLE);
			}else {//直接从个人中心的地址中是无法进入到此步的。
				type = 2;
				initImage();
				titleRight.setText("编辑");
				getAddressList();
			}
			break;
		case R.id.ll_address_1:
			if (type == 1) {
				Intent itAd1 = new Intent(mContext, AddressModifyActivity.class);
				itAd1.putExtra("keytype", "1");
				itAd1.putExtra("address", address1);
				startActivityForResult(itAd1, REQUEST_MOD);
			} else {
				if (address1 == null) {
                    XtomToastUtil.showShortToast(mContext, "请添加收货地址");
					break;
				}
				setImages(0);
//				titleText.postDelayed(new Runnable() {
//					
//					@Override
//					public void run() {
						Intent it1 = new Intent();
						it1.putExtra("address", address1);
						setResult(RESULT_OK, it1);
						finish();
//					}
//				}, 500);
			}
			break;
		case R.id.ll_address_2:
			if (type == 1) {
				Intent itAd2 = new Intent(mContext, AddressModifyActivity.class);
				itAd2.putExtra("keytype", "1");
				itAd2.putExtra("address", address2);
				startActivityForResult(itAd2, REQUEST_MOD);
			} else {
				if (address2 == null) {
                    XtomToastUtil.showShortToast(mContext, "请添加收货地址");
                    break;
                }
				setImages(1);
//				titleText.postDelayed(new Runnable() {
//					
//					@Override
//					public void run() {
						Intent it2 = new Intent();
						it2.putExtra("address", address2);
						setResult(RESULT_OK, it2);
						finish();
//					}
//				}, 500);
			}
			break;
		case R.id.ll_address_3:
			if (type == 1) {
				Intent itAd3 = new Intent(mContext, AddressModifyActivity.class);
				itAd3.putExtra("keytype", "1");
				itAd3.putExtra("address", address3);
				startActivityForResult(itAd3, REQUEST_MOD);
			} else {
				if (address3 == null) {
                    XtomToastUtil.showShortToast(mContext, "请添加收货地址");
                    break;
                }
				setImages(2);
//				titleText.postDelayed(new Runnable() {
//					
//					@Override
//					public void run() {
						Intent it3 = new Intent();
						it3.putExtra("address", address3);
						setResult(RESULT_OK, it3);
						finish();
//					}
//				}, 500);
			}
			break;
		default:
			break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (RESULT_OK != resultCode) {
			return;
		}
		switch (requestCode) {
		case REQUEST_MOD:
			getAddressList();
			break;
		default:
			break;
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

}
